{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第5章 变量与运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5-2 变量定义\n",
    "\n",
    "+ 变量是区分大小写地\n",
    "+ 变量和js类似可以随意改变变量类型，即变量是动态地"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5-3 值类型和引用类型\n",
    "\n",
    "+ int、str、tuple属于值类型，是不可改变的\n",
    "+ list、set、dict属于引用类型，是可改变的。\n",
    "\n",
    "不可改变的变量类型当赋新值时实际是改变了内存地址，用`id()`可以查看\n",
    "\n",
    "+ 将值类型变量赋值给新变量后，若改变原变量则是使原变量指向新的值，新变量仍然指向原值不变；\n",
    "+ 将引用类型赋值给新变量时，新旧变量都是引用同一值，此时修改旧变量的值时新变量同样改变。但若对旧变量重新定义或整体赋值，则新变量仍然引用原值不发生变化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1', 2, 3]\n",
      "['1', 2, 3]\n",
      "{'name': '梁山广', 'age': 27}\n",
      "['1', 2, 3]\n"
     ]
    }
   ],
   "source": [
    "# 测试list的引用属性\n",
    "a = [1, 2, 3]\n",
    "b = a\n",
    "a[0] = '1' # 对旧变量a局部赋值，新变量b也会变\n",
    "print(a)\n",
    "print(b) # b也会被改变\n",
    "a = {'name':'梁山广', 'age':27} # 对旧变量a整体赋值，新变量b不会变\n",
    "print(a)\n",
    "print(b) # b不会被改变"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5-3 列表list的可变与元祖tumple的不可变\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "140578240670152"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_a = [1, 2, 3]\n",
    "id(list_a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "140578240670152"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_a[0] = 'sds' # 局部赋值不改变list地址\n",
    "id(list_a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "140578020142896"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_a = (1, 2, 3) # 整体赋值改变list_a的地址\n",
    "id(list_a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_b = [1, 2, 3]\n",
    "list_b.append(4)\n",
    "list_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple_a = (1, 2, 3, ['a', 'b', 'c'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple_a[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple_a[3][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-16-0f4a6251e296>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtuple_a\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m4\u001b[0m \u001b[0;31m# tuple元素初始化后就不能变了\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "tuple_a[2] = 4 # tuple元素初始化后就不能变了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5-5 ~ 5-13 运算符\n",
    "\n",
    "### 成员运算符\n",
    "\n",
    "+ `in` : `1 in [1, 2, 3] # True`\n",
    "+ `not in` : `'d' in ('a', 'b', 'c') # False`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 in [1, 2, 3] # True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'d' in ('a', 'b', 'c') # False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'d' not in ('a', 'b', 'c') # True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 身份运算符\n",
    "\n",
    "+ is\n",
    "+ is not\n",
    "\n",
    "返回的结果也是bool \n",
    "\n",
    "+ is和==的区别\n",
    "  + == 比较的是两个变量的值\n",
    "  + is 比较的是两个变量的身份(内存地址)是否相等\n",
    "\n",
    "+ 举例\n",
    "  + a={1,2,3}, b={1,3,2}  由于集合是无序的，所以a==b为True，而a is b是False\n",
    "  + a=(1,2,3), b=(1,3,2)  由于元祖是有序的，所以a==b为False，同时a is b也是False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "set_a = {1, 2, 3}\n",
    "set_b = {1, 3, 2}\n",
    "\n",
    "print(set_a == set_b)\n",
    "print(set_a is set_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "tumple_a = (1, 2, 3)\n",
    "tumple_b = (1, 3, 2)\n",
    "print(tumple_a == tumple_b)\n",
    "print(tumple_a is tumple_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 1\n",
    "isinstance(a, int) # 判断a是否是整型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6-9 常量与编码规范\n",
    "\n",
    "+ 1、python没有常量概念，但是我们可以用大写的方式区分\n",
    "+ 2、一个文件就是一个模块(module)，一个python程序由多个模块组成，而模块建议增加说明文档(注释)\n",
    "+ 3、python对常量的定义是当变量不存在任何函数和类中，就是常量。所以变量尽量封装在函数或类里\n",
    "+ 4、标识符前不要加空格\n",
    "+ 5、程序末尾要空出一行(可选)\n",
    "+ 6、比较运算符两侧需各空一格，否则可能报错\n",
    "+ 7、缩进为四个空格，而不是Tab键(有些IDE不支持Tab键)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6-11 条件控制语句\n",
    "\n",
    "```python\n",
    "if    条件判断1\n",
    "      打印结果\n",
    "elif  条件判断2\n",
    "      打印结果\n",
    "else:\n",
    "      打印结果\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7-2 for与for else\n",
    "\n",
    "1、for循环主要是用来遍历/循环，序列或者集合、字典使用；\n",
    "\n",
    "```python\n",
    "a = [['apple','orange','banana','grape'],(1,2,3)]\n",
    "\n",
    "for x in a:\n",
    "    for y in x:\n",
    "        print(y)\n",
    "else:\n",
    "    print('fruit is gone')\n",
    "```\n",
    "\n",
    "当for全部遍历完成后，才会执行到else；\n",
    "+ 如果执行的是break，则不会再继续执行eof，因为此时程序已被强制终止\n",
    "+ 如果执行的是continue,则继续执行eof，此时程序还在继续执行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "orange\n",
      "banana\n",
      "grape\n",
      "1\n",
      "2\n",
      "3\n",
      "fruit is gone\n"
     ]
    }
   ],
   "source": [
    "a = [['apple','orange','banana','grape'],(1,2,3)]\n",
    "\n",
    "for x in a:\n",
    "    for y in x:\n",
    "        print(y)\n",
    "else:\n",
    "    print('fruit is gone')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7-3 for与range\n",
    "\n",
    "+ 1.for限制范围不需要用i：\n",
    "  ```python\n",
    "  for x in range(0,10):\n",
    "    print(x)\n",
    "  ```  \n",
    "+ 2.`range(0,10)`  \n",
    "  注意10代表的不是终止数，而是偏移量。从0开始，总共10个数字。\n",
    "+ 3.递增等差数列：\n",
    "  ```python\n",
    "  for x in range(0,10,2): # 2是间隔几个数字\n",
    "    print(x, end='|')\n",
    "  ```\n",
    "  ```shell\n",
    "  output：0|2|4|6|8|\n",
    "  ```\n",
    "\n",
    "+ 4.递减等差数列：\n",
    "  ```python\n",
    "  for x in range(10,0,-2):\n",
    "    print(x, end='|')\n",
    "  ```\n",
    "+ 5.课后练习：\n",
    "  ```python\n",
    "  for x in range(1,8,2):\n",
    "    print(x, end='|')\n",
    "  ```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0|2|4|6|8|"
     ]
    }
   ],
   "source": [
    "for x in range(0, 10, 2):\n",
    "    print(x, end='|')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0|2|4|6|8|"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "for x in range(0, len(a), 2):\n",
    "    print(x, end = '|')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
